﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using System.Data;
using System.Text;
using Runda.EShop.Common.Component;

namespace Runda.EShop.SaaS.OrderManage
{
    public partial class OrderDelete : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            InitProcess();
            GetOrderDetail();
        }

        private void InitProcess()
        {
            string strFormat1 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li><li class=\"tx2\">{1}</li><li class=\"tx3\">{2}</li></ul></div>";
            string strFormat2 = "<div class=\"{0}\"><ul><li class=\"tx1\">&nbsp;</li></ul></div>";

            BizOrderStatusBll bll = new BizOrderStatusBll();
            DataTable dt = bll.GetAllOrderStatus(OrderID);
            StringBuilder sb = new StringBuilder();
            bool flag = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == dt.Rows.Count - 1)
                {
                    if (Convert.ToInt32(dt.Rows[i]["ProcessStatus"].ToString()) == 0)
                    {
                        sb.AppendLine(string.Format(strFormat1, "node wait", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                    }
                    else
                    {
                        sb.AppendLine(string.Format(strFormat1, "node ready", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                    }
                }
                else
                {
                    if (Convert.ToInt32(dt.Rows[i]["ProcessStatus"].ToString()) == 0 && flag)
                    {
                        sb.AppendLine(string.Format(strFormat1, "node singular", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                        sb.AppendLine(string.Format(strFormat2, "proce wait"));
                        flag = false;
                    }
                    else
                    {
                        if (Convert.ToInt32(dt.Rows[i]["ProcessStatus"].ToString()) == 0)
                        {
                            sb.AppendLine(string.Format(strFormat1, "node wait", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                            sb.AppendLine(string.Format(strFormat2, "proce wait"));
                        }
                        else
                        {
                            sb.AppendLine(string.Format(strFormat1, "node ready", dt.Rows[i]["StatusName"].ToString(), dt.Rows[i]["CreatedAt"].ToString()));
                            sb.AppendLine(string.Format(strFormat2, "proce ready"));
                        }
                    }
                }

            }

            ltlProcess.Text = sb.ToString();
        }

        private void GetOrderDetail()
        {
            if (OrderID == Guid.Empty) return;
            BizOrderDetailBll bll = new BizOrderDetailBll();
            DataTable dt = bll.GetOrderDetail(OrderID);
            gvOrderDetail.DataSource = dt;
            gvOrderDetail.DataBind();
        }

        protected void gvOrderDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid DetailID = new Guid(gvOrderDetail.DataKeys[e.RowIndex]["DetailID"].ToString());

            BizOrderDetailBll bll = new BizOrderDetailBll();
            bll.Delete<BizOrderDetailEntity>(" and DetailID = '" + DetailID + "'");

            GetOrderDetail();
        }

        protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dv = e.Row.DataItem as DataRowView;
                DropDownList ddlUnit = e.Row.FindControl("ddlUnit") as DropDownList;
                ddlUnit.SelectedValue = dv["Unit"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            OrderBll bll = new OrderBll();
            bll.CancelOrder(OrderID, base.User.Identity.LoginUser, txtReason.Text);

            ClientScript.RegisterStartupScript(this.GetType(), "asdf", "alert('保存成功.');window.close();", true);
        }
    }
}